package com.web;

import com.TestClass;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dto.Staff;
import com.dto.Student;
import com.dto.User;
import com.service.WebService;
import com.service.WebServiceImpl;
import com.tolls.E;
import com.tolls.JavaUtills;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Created by 黑旋风诸葛亮 on 2018/1/30.
 */

@Controller
@RequestMapping({"/web"})
public class WebController {
    //
    @Autowired
    private WebService service;
    @Autowired
    private HttpSession session;




// 登陆验证
    @ResponseBody
    @RequestMapping(value = "/land", method = RequestMethod.POST)
    public String land(User user) {

//        try {

          User us = service.checkLand(user);
          if(us !=null){
              return "1" ;
          }else{
              return "2";
          }

//        } catch (Exception e) {
//
//            return 2;
//        }
    }






    @ResponseBody
    @RequestMapping(value = "/jj", method = RequestMethod.GET)
    public String jj() {

        try {


            System.out.println("jjjjjjjjjjjjjjj");

            Staff staff = new Staff();
            staff.setName("ioeooln");
            session.setAttribute("staff",staff);
            service.message();
            JSONObject json = new JSONObject();
            json.put("sh", "show");
            return (String) session.getAttribute("name");
        } catch (Exception e) {

            return (String) session.getAttribute("name");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/ll", method = RequestMethod.GET)
    public String ll() {
        try {

            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            Student s = (Student) request.getSession().getAttribute("user");
            request.getCookies();
            System.out.println(s);
            request.getHeader("authorization");
            System.out.println(request.getHeader("authorization"));
            System.out.println(request.getSession());
            System.out.println("jjjjjjjjjjjjjjj");

            service.message();
            JSONObject json = new JSONObject();
            json.put("sh","show");
            return "lllllllll";
        }
        catch (Exception e){

            return "jjjjjjjjj";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/post",method = RequestMethod.POST)
    public JSON post( Student student){


        try{
            System.out.println(student);
            System.out.println("jjjjjjjjjjjjjjj");
            service.message();
            JSONObject json = new JSONObject();
            json.put("sh","show");
            return json;
        }
        catch (Exception e){

            return null;
        }
    }

//    @ResponseBody
//    @RequestMapping(value = "/ref",method = RequestMethod.GET)
//    public JSON ref(){
//
//
//        JSON js = new JSON() {
//            @Override
//            public String toString() {
//                return super.toString();
//            }
//        }
//    }


  //  @ResponseBody
    //@RequestMapping({"test"})
   @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String testMessage(Model model){

        System.out.println("jjjjjjjjjjjjjjj");
        model.addAttribute("jie","deio");
      /// service.message();
       return "jsp/show.jsp";
    }

    //  @ResponseBody
    //@RequestMapping({"test"})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {

      //  System.out.println("jjjjjjjjjjjjjjj");
        System.out.println("viewList");
        System.out.println("view");
        System.out.println("sout view");
        //  System.out.println("jjjjjjjjjjjjjjj");
 // /Users/v_xinqingqiang/Desktop/appSpace/demo0130/web/jsp/show.jsp
        return "web/jsp/show";
    }



    //文件上传
    @ResponseBody
    @RequestMapping(value = "/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file) {


        String url = file.getFileItem().getName();
        String fullName = url.substring(url.lastIndexOf("\\") + 1, url.length()); // 文件名称
        String suffix = fullName.substring(fullName.lastIndexOf(".") + 1);
        String uuid = String.valueOf(UUID.randomUUID()).replace("-", "");
        String originalName = uuid + "." + suffix;

        service.uploadAttach(file ,originalName);

        return "fileUpload";

    }

    @ResponseBody
    @RequestMapping("split")
    public  static  List<String> split(){

        String s = "1,2,3,4,5,6,7,8,9,";

        String[] array = s.split(",");
        List<String> ar = Arrays.asList(array);

        System.out.println(ar);
        return  ar;
    }


    public static void main(String[] args) {

        System.out.println("提交1");
        System.out.println("提交2");



    }





    @ResponseBody
    @RequestMapping("up")
    public String up(@RequestParam("file") CommonsMultipartFile file) {


        System.out.println(file);
        System.out.println("jjjj");

        return  null;
    }
        //这是一个master的tag




}